﻿using Twitter.Tests;
using System;
using NUnit.Framework;
using FriendsNearbyBot;
using FriendsNearby.Services;
using Twitter;
using System.Linq;
using Moq;
using System.ComponentModel.Design;
using FriendsNearby.Services.Entities;
using System.Diagnostics;
using FriendsNearbyBot.Tests;
using System.Linq.Expressions;

[TestFixture]
public class PeopleNearbyProcessorFixture : TestBase
{
    IServiceProvider services;        
    Mock<IPeopleNearbyService> peopleNearbyService;
    Mock<IUsersService> usersService;
	Guid me = Guid.Empty;

    [SetUp]
    public void CreateServices()
    {
        var services = new ServiceContainer();
        this.services = services;

        peopleNearbyService = new Mock<IPeopleNearbyService>();
        usersService = new Mock<IUsersService>();

        services.AddService(peopleNearbyService.Object);
        services.AddService(usersService.Object);
    }

    [Test]
    public void ICanSeeMyFriendsNearby()
    {
		usersService.Expect(x => x.QueryUserRegistry(It.IsAny<Expression<Func<UserRegistry, bool>>>())).Returns(new UserRegistry { UserID = me });
			
        peopleNearbyService.Expect(x => x.GetContacts(me, QueryContactsKind.Nearby)).Returns(new PersonNearby[]{
            new PersonNearby() { DisplayName = "user1" , Distance = 1 },
            new PersonNearby() { DisplayName = "user2" , Distance = 2 }
        });

        // Warn: 
        MemoryBotClient bot = new MemoryBotClient("fnb");
        MemoryBotClient user = new MemoryBotClient("fnbtestb");
        bot.SetBuddy(user);
        user.SetBuddy(bot);

        PeopleNearbyProcessor processor = new PeopleNearbyProcessor(usersService.Object, peopleNearbyService.Object, CreateMessageBuilder());
        processor.Process(bot, CreateMessage("list near", user.ScreenName, bot.ScreenName));
        
        Assert.AreEqual(1, user.Messages.GetAll().Count());
        AssertString.StartsWith("user1:1 Km.;",user.Messages.GetAll().First().Text);
    }
}
